<?php

class HelpCatController extends BaseController
{

	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionCreate()
	{
		$model=new HelpCat;

		$model->zhc_title = trim($_GET['title']);
		$model->zhc_parentid = (int)$_GET['pid'];
		
		if($model->save()){
			//成功跳转
			$this->redirect(array('admin'));
		}else{
			//失败跳转
			$this->render('../index/error',array(
					'message'=>'对不起，帮助类目创建失败，请稍后再试！',
					'links'=>array(
							array('帮助类目管理',$this->pre_module_url.'/helpCat/admin'),
					),
					));
			exit;
		}
	}

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate()
	{
		$newsCatId = $_POST['HelpCat']['zhc_id'];
		$newsCatTitle = $_POST['HelpCat']['zhc_title'];
		$helpdescribe = $_POST['HelpCat']['zhc_desc'];
		foreach($newsCatId as $key=>$val){
			$model = $this->loadModel($val);
			$model->zhc_title = $newsCatTitle[$key];
			$model->zhc_desc = $helpdescribe[$key];
			$model->zhc_listorder = $key;
			if(!$model->save()){
				//失败跳转
				$this->render('../index/error',array(
						'message'=>'对不起，帮助类目修改失败，请稍后再试！',
						'links'=>array(
								array('帮助类目管理',$this->pre_module_url.'/helpCat/admin'),
						),
						));
				exit;
			}
		}
		//成功跳转
		$this->redirect(array('admin'));
	}

	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'admin' page.
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete($id)
	{
		$model = $this->loadModel($id);
		//分类下有子分类，不能删除
		if(!empty($model->zhc_childid)){
			//失败跳转
			$this->render('../index/error',array(
					'message'=>'对不起，帮助类目有子分类，不能删除！',
					'links'=>array(
							array('帮助类目管理',$this->pre_module_url.'/helpCat/admin'),
					),
					));
			exit;
		}
		
		if(Help::model()->findByAttributes(array('zhc_id'=>$id))){
			//失败跳转
			$this->render('../index/error',array(
					'message'=>'对不起，类目下有帮助信息，请先删除或转移信息！',
					'links'=>array(
							array('帮助类目管理',$this->pre_module_url.'/helpCat/admin'),
					),
					));
			exit;
		}
		
		if($model->delete()){
			//成功跳转
			$this->render('../index/success',array(
					'message'=>'恭喜，帮助类目删除成功',
					'links'=>array(
							array('帮助类目管理',$this->pre_module_url.'/helpCat/admin'),
					),
					));
			exit;
		}else{
			//失败跳转
			$this->render('../index/error',array(
					'message'=>'对不起，帮助类目删除失败，请稍后再试！',
					'links'=>array(
							array('帮助类目管理',$this->pre_module_url.'/helpCat/admin'),
					),
					));
			exit;
		}	
	}


	/**
	 * Manages all models.
	 */
	public function actionAdmin()
	{
		$model=new HelpCat('search');
		$model->unsetAttributes();  // clear any default values
		$model->zhc_parentid = 0;//一级分类
		if(isset($_GET['HelpCat']))
			$model->attributes=$_GET['HelpCat'];

		$this->render('admin',array(
			'model'=>$model,
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=HelpCat::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='help-cat-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}
